package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try {
            //新增套餐成功
            setmealService.add(setmeal,checkgroupIds);

            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS) ;
        } catch (Exception e) {
            //新增套餐失败
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件名后缀
            String suffx = originalFilename.substring(lastIndexOf - 1);
            //使用uuid随机产生文件名称，防止回名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffx;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            //将上传图片名称存入redis，基于redis的set集合储存
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            //更改套餐成功
            setmealService.update(setmeal,checkgroupIds);
            return new Result(true,"更改套餐信息成功") ;
        } catch (Exception e) {
            //更改套餐失败
            e.printStackTrace();
            return new Result(false,"更改套餐信息失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            //删除套餐成功
            setmealService.delete(id);
            return new Result(true,"删除套餐成功") ;
        } catch (Exception e) {
            //删除套餐失败
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            //查询套餐成功
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal) ;
        } catch (Exception e) {
            //查询套餐失败
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findcheckgroupidbysetmealid")
    public Result findcheckgroupIds(@RequestParam Integer setmealId){
        try {
            List<Integer> checkgroupIds = setmealService.findcheckgroupIds(setmealId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
